from django.shortcuts import render
from .Cloud import *
from django.http import JsonResponse
from .models import *
import json
# Create your views here.
#查询词云图地址并返回
def search_cloud(request)->str:
    response = {}
    if request.method == "POST" and request.body:
        name = json.loads(request.body)['name']
        path = cloud_path(name)
        response['path'] = path
    return JsonResponse(response)
#将国家占比数据返回
def country_ratio(request)->str:
    response = {}
    if request.method == "GET" and request.GET:
        year = request.GET['year']
        response['data'] = req_ratio(year)
    return JsonResponse(response)
#将年份与类型数据返回
def type_year(request)->str:
    response = {}
    result = []
    if request.method == "GET":
        data = req_type()
        #将数据格式处理一下，方便前端展示
        for i in data:
            #用来存储每一条数据
            detail = {}
            detail['name'] = i['name']
            detail['type'] = 'line'
            detail['stack'] = 'Total'
            #将得到的数据转化为列表
            detail['data'] = list(i.values())[2:]
            result.append(detail)
        response['data'] = result
    return JsonResponse(response)
            
            
        
